<template>
  <div style="background: #fff;margin: 10px 10px 0px 10px;border-radius: 10px;">
    <el-row class="app-toptool" type="flex">
     <!-- <el-col :span="16">
        <Search
          size="small"
          :search-visible="searchVisible"
          :search-data.sync="searchData"
          :search-form="searchForm"
          @refesh_list="getList"
        />
      </el-col> -->
      <el-col :span="8">
        <div class="btn-group" style="margin-bottom: 11px; float: right">
          <div>
            <el-button
              v-if="checkPermission('/memberauthgroup/add')"
              type="primary"
              size="small"
              
              @click="add()"
              >添加</el-button
            >
            <!-- <el-button
              v-if="checkPermission('/memberauthgroup/delete')"
              type="primary"
              :disabled="multiple"
              size="small"
              icon="el-icon-delete"
              @click="del(ids)"
            >删除
            </el-button> -->
          </div>
        </div>
      </el-col>
    </el-row>
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :row-class-name="rowClass"
      :tree-props="{ children: 'children' }"
      :default-expand-all="expand"
      row-key="id"
      :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
      :border="false"
      :stripe="true"
      class="eltable"
      :data="list"
      style="width: 100%"
      @selection-change="selection"
    >
      <!-- <el-table-column align="center" type="selection" width="42" /> -->
      <el-table-column
        align="center"
        type=""
        property="id"
        label="ID"
        show-overflow-tooltip
      />
      <el-table-column
        align="left"
        property="title"
        label="名称"
        show-overflow-tooltip
      />
      <el-table-column
        align="left"
        property="discount"
        label="会员折扣(%)"
        show-overflow-tooltip
      />
      <el-table-column
        align="left"
        property="upgrademoney"
        label="升级价格"
        show-overflow-tooltip
      />
     <!-- <el-table-column
        align="left"
        property="description"
        label="描述信息"
        show-overflow-tooltip
      /> -->
      <el-table-column
        align="center"
        property="is_buyright"
        label="商品购买权限"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.is_buyright"
            :active-value="1"
            :inactive-value="0"
            @change="listUpdate(scope.row, 'is_buyright')"
          />
        </template>
      </el-table-column>
     <!-- <el-table-column
        align="center"
        property="is_lookprice"
        label="能否查看商品价格"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.is_lookprice"
            :active-value="1"
            :inactive-value="0"
            @change="listUpdate(scope.row, 'is_lookprice')"
          />
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        align="center"
        property="is_default"
        label="默认"
        show-overflow-tooltip
        width=""
      >
        <template slot-scope="scope">
          <el-switch
            :disabled="true"
            v-model="scope.row.is_default"
            :active-value="1"
            :inactive-value="0"
            @change="listUpdate(scope.row,'is_default')"
          />
        </template>

      </el-table-column> -->
      <el-table-column
        align="center"
        property="status"
        label="状态"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            @change="listUpdate(scope.row, 'status')"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column
        align="center"
        property="sort"
        label="排序"
        show-overflow-tooltip
      >
        <template slot-scope="scope" class="sort">
          {{ scope.row.status == 1 ? "显示" : "隐藏" }}
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <div v-if="scope.row.id">
            <el-button
              v-if="checkPermission('/memberauthgroup/update')"
              size="mini"
              type="primary"
              @click="update(scope.row)"
              >编辑</el-button
            >
            <el-button
              v-if="checkPermission('/memberauthgroup/delete')"
              size="mini"
              type="danger"
              @click="del(scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div style="height: 60px" />
    <!--添加/修改-->
    <Update
      :info="updateInfo"
      :opentype="opentype"
      :show.sync="dialog.updateDialogStatus"
      size="small"
      @refesh_list="getList"
    />
  </div>
</template>

<script>
import Search from "@/components/common/Search";
import Update from "@/views/memberauthgroup/update.vue";
import waves from "@/directive/waves"; // waves directive
import { confirm, param2Obj } from "@/utils/common";
export default {
  name: "Memberauthgroup",
  directives: { waves },
  components: {
    Search,
    Update,
  },
  data() {
    return {
      dialog: {
        updateDialogStatus: false,
      },
      ids: [],
      single: true,
      multiple: true,
      list: [],
      opentype: "add",
      updateInfo: {},
      loading: false,
      searchVisible: true,
      searchForm: [],
      searchData: {},
      expand: true,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      const param = {};
      Object.assign(param, this.searchData);
      Object.assign(param, param2Obj(this.$route.fullPath));
      this.loading = true;
      this.$api.post("/memberauthgroup/index", param).then((res) => {
        this.list = res.data;
        this.loading = false;

        this.searchForm = [
          {
            type: "Input",
            label: "请输入查询内容",
            prop: "keyword",
            width: "230px",
          },
        ];
      });
    },
    listUpdate(row, field) {
      if (row.id) {
        this.$api
          .post("/memberauthgroup/listUpdate", {
            id: row.id,
            [field]: row[field],
          })
          .then((res) => {
            this.$message({
              message: "操作成功",
              type: "success",
            });
          });
      }
    },
    add() {
      this.opentype = "add";
      this.dialog.updateDialogStatus = true;
    },
    update(row) {
      this.opentype = "update";
      const id = row.id ? row.id : this.ids.join(",");
      this.$api
        .post("/memberauthgroup/getInfo", {
          id: id,
        })
        .then((res) => {
          this.dialog.updateDialogStatus = true;
          this.updateInfo = res.data;
        });
    },
    del(row) {
      confirm({
        content: "确定要操作吗",
      })
        .then(() => {
          const ids = row.id ? row.id : this.ids.join(",");
          this.$api
            .post("/memberauthgroup/delete", {
              id: ids,
            })
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.getList();
            });
        })
        .catch(() => {});
    },
    selection(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    rowClass({ row, rowIndex }) {
      for (let i = 0; i < this.ids.length; i++) {
        if (row.id === this.ids[i]) {
          return "rowLight";
        }
      }
    },
    toggleRowExpansion() {
      this.expand = !this.expand;
      this.list.forEach((item) => {
        this.$refs.multipleTable.toggleRowExpansion(item, this.expand);
      });
    },
  },
};
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}

.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}

.el-col-8 {
    width: 8%;
}
</style>
